import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";


// Basics
import Login from "../views/login/login.vue";
import Register from "../views/login/register.vue";
import Login2 from "../views/user/login.vue";
import Register2 from "../views/user/register.vue";
import Personal from "../views/personal/personal.vue";
import Home from "../views/Home.vue";

// Details page
import Mark from "../views/markdown/Submitmd.vue";
import EditPost from "../views/markdown/Editpostmd.vue";
import PostDetails from "../views/details/post.vue";

import MusicDetails from "../views/details/music.vue";

// Other
import submitSuccess from "../views/other/SubSuccess.vue";
import deleteSuccess from "../views/other/DelSuccess.vue";
import notfound from "../views/other/404.vue";
import NoAuthorized from "../views/other/403.vue";


// viewer
import PDF from "../views/PDFviewer/PDF.vue"

const routes: Array<RouteRecordRaw> = [
  {
    path: "/search",
    component: Home,
  },
  {
    path: "/search/:category",
    component: Home,
  },
  {
    path: "/md",
    component: Mark,
    meta: {
      needLogin: true,
    },
  },
  {
    path: "/edit/post/:category",
    component: EditPost,
    meta: {
      needLogin: true,
    },
  },
  {
    path: "/login",
    component: Login2,
  },
  {
    path: "/register",
    component: Register2,
  },
  {
    path: "/post/:category",
    component: PostDetails,
  },
  {
    path: "/music/:category",
    component: MusicDetails,
  },
  {
    path: "/personal/:category",
    component: Personal,
  },
  {
    path: "/submitSuccess/:category",
    component: submitSuccess,
  },
  {
    path: "/deleteSuccess/:category",
    component: deleteSuccess,
  },
  {
    path: "/404",
    component: notfound,
  },
  {
    path: "/403",
    component: NoAuthorized,
  },
  {
    path: "/",
    redirect: "/search/post",
  },
  {
    path: "/:catchAll(.*)",
    redirect: "/404",
  },
  // {
  //   path: "/PDF",
  //   component:PDF,
  // },
];
const router = createRouter({
  history: createWebHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  const id = localStorage.getItem("id") ;
  if((id == "" || id == null )&& to.meta.needLogin)
  {
    //未登录403
    router.push("/403")
  }else{
    next();
  }
});

export default router;
